<?php

namespace App\Http\Controllers\Home;

use App\Home\Article;
use App\Home\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class ArticleController extends Controller
{
   //文章初始页面
    public  function Index(){
        $id = session('user_id');
        $res = Article::get()->toArray();
        $rec = DB::table('users')->where('id',$id)->join('headpics','user_id','=','id')->select('users.*','headpics.head')
            ->get();
       return view('home.articleadd',['res'=>$res,'rec'=>$rec]);
   }
   //创建文章
    public  function  Create(){

        return view('home.articlecreate');
    }
    //添加文章
    public function Add(Request $request){
        //dd($request->all());
        $id = $request->id;
        $title = $request->title;
        $content = $request->contents;
        $file = $request->pic->getClientOriginalExtension();
        $pic = 'title'.time().'.'.$file;
        $time = date('Y-m-d H:i:s',time());
        //dd($time);
        $request->pic->move('images',$pic);
        //dd($pic);
        $res = Article::insert(['user_id'=>$id,'title'=>$title,'content'=>$content,'pic'=>$pic,'created_at'=>$time]);
        if ($res){
            return redirect('user/articlelist');
        }
    }
    //文章列表
    public function Show(){
        $id = session('user_id');
//        dump($id);
        $res = Article::get()->toArray();
        $rec = DB::table('users')->where('id',$id)->join('headpics','user_id','=','id')->select('users.*','headpics.head')
        ->get();
       //dd($rec);
       return view('home.articlelist',['res'=>$res,'rec'=>$rec]);
   }
   //文章修改
    public function Edit(){
        $id = $_GET['id'];
        $res = Article::where('id',$id)->get()->toArray();
//        dd($res);
        $uid = session('user_id');
        $rec = DB::table('users')->where('id',$uid)->join('headpics','user_id','=','id')->select('users.*','headpics.head')
            ->get();
        return view('home.articleedit',['res'=>$res,'rec'=>$rec]);
    }


    public function Edited(Request $request){
        $id = $request->id;
//        dd($id);
        $title = $request->title;
        $content = $request->contents;
        $time = date('Y-m-d H:i:s',time());
        $file = isset($request->pic)?$request->pic:'';
        if ($file == ''){
            $res = Article::where('id',$id)->update(['title'=>$title,'content'=>$content,'updated_at'=>$time]);
        }else{
            $file = $request->pic->getClientOriginalExtension();
            $pic = 'title'.time().'.'.$file;
            $request->pic->move('images',$pic);
            $res = Article::where('id',$id)->update(['title'=>$title,'content'=>$content,'pic'=>$pic,'updated_at'=>$time]);
        }
        if ($res != false){
            return redirect('user/articlelist');
        }

    }
    //文章删除
    public function Del(){
        $id = $_GET['id'];
        $res = Article::where('id',$id)->delete();
        if ($res != false){
            return redirect('user/articlelist');
        }
    }
}
